package cn.edu.zucc.usedcarsales.control;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.edu.zucc.usedcarsales.itf.IVehicleTypeManager;
import cn.edu.zucc.usedcarsales.model.VehicleType;
import cn.edu.zucc.usedcarsales.util.DBUtil;

public class VehicleTypeManager implements IVehicleTypeManager {

	@Override
	public List<VehicleType> getAllType() {
		// TODO Auto-generated method stub
		List<VehicleType> typeList = new ArrayList<VehicleType>();
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = con.prepareStatement("select * from VehicleType");
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				VehicleType vt = new VehicleType(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4));
				typeList.add(vt);
			}
			return typeList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return typeList;
		}
	}

	@Override
	public void addType(VehicleType type) {
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = con.prepareStatement("insert into VehicleType (VehicleTypeName,VehicleTypeIntroduction,VehicleAdvantage) values(?,?,?)");
			pst.setString(1, type.getVehicleTypeName());
			pst.setString(2, type.getVehicleTypeIntroduction());
			pst.setString(3, type.getVehicleAdvantage());
			pst.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void deleteType(int id) {
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = con.prepareStatement("delete from VehicleType where VehicleTypeID = ?");
			pst.setInt(1, id);
			pst.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public VehicleType getType(int  id) {
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = con.prepareStatement("select * from  VehicleType where VehicleTypeID=?");
			pst.setInt(1, id);
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				VehicleType vt = new VehicleType(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4));
				return vt;
			}
			return null;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public void updateType(VehicleType type) {
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = con.prepareStatement("update VehicleType set VehicleTypeName=?,VehicleTypeIntroduction=?,VehicleAdvantage=? where VehicleTypeID=?");
			pst.setString(1, type.getVehicleTypeName());
			pst.setString(2, type.getVehicleTypeIntroduction());
			pst.setString(3, type.getVehicleAdvantage());
			pst.setInt(4, type.getVehicleTypeID());
			pst.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public String getTypeNameById(int typeId) {
		// TODO Auto-generated method stub
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = con.prepareStatement("select * from VehicleType where VehicleTypeID=?");
			pst.setInt(1, typeId);
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				return rs.getString("VehicleTypeName");
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return "";
		}
		return "";
	}

	@Override
	public int getIdByName(String name) {
		// TODO Auto-generated method stub
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = con.prepareStatement("select * from VehicleType where VehicleTypeName=?");
			pst.setString(1, name);
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				return rs.getInt("VehicleTypeID");
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
		return 0;
	}

}
